package com.ck.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Chapter;
import com.ck.domain.Xuanze;
import com.ck.service.QuestionService;

public class EditXuanzeInfoController extends SimpleFormController {
	
	
	private QuestionService questionService;
	
	//设置公共变量，供显示表单和回显表单时使用;
	private List<Chapter> chapterList = null;
	private String pic_url = null;

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {		
		Map model = new HashMap();
		chapterList = questionService.gainChapterList();
		
		Integer xuanzeID = Integer.parseInt(request.getParameter("xuanzeID"));
		Xuanze xuanze = questionService.gainXuanze(xuanzeID);

		if(xuanze.getPic()!=null){
			pic_url = questionService.showImage(request, xuanze.getPic());
		}
		
		model.put("chapterList", chapterList);
		model.put("pic_url", pic_url);
		
		return model;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		Integer xuanzeID = Integer.parseInt(request.getParameter("xuanzeID"));
		Xuanze xuanze = questionService.gainXuanze(xuanzeID);
		return xuanze;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Integer xuanzeID = Integer.parseInt(request.getParameter("xuanzeID"));
		Xuanze xuanze = (Xuanze) command;
		xuanze.setId(xuanzeID);
		
		Map model = errors.getModel();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

		//后台校验
		boolean flag = true;
		String description = xuanze.getDescription().trim();
		String correct_answer = xuanze.getCorrect_answer().trim();
		String false_one = xuanze.getFalse_one().trim();
		String false_two = xuanze.getFalse_two().trim();
		String false_three = xuanze.getFalse_three().trim();
		Integer difficulty = xuanze.getDifficulty();
		Integer score = xuanze.getScore();
		Integer chapterID = xuanze.getChapter();
		String remark = xuanze.getRemark().trim();
		
		if(description == null || "".equals(description)) {
			model.put("error_description", "请输入题目描述");
			flag = false;
		}
		
		if(description.length() > 500) {
			model.put("error_description", "题目描述长度：1-500个字符");
			flag = false;
		}
		
		if(correct_answer == null || "".equals(correct_answer)) {
			model.put("error_correct_answer", "请输入正确答案");
			flag = false;
		}
		
		if(correct_answer.length() > 250) {
			model.put("error_correct_answer", "正确答案长度：1-250个字符");
			flag = false;
		}
		
		if(false_one == null || "".equals(false_one)) {
			model.put("error_false_one", "请输入错误答案1");
			flag = false;
		}
		
		if(false_one.length() > 250) {
			model.put("error_false_one", "错误答案1长度：1-250个字符");
			flag = false;
		}
		
		if(false_two == null || "".equals(false_two)) {
			model.put("error_false_two", "请输入错误答案2");
			flag = false;
		}
		
		if(false_two.length() > 250) {
			model.put("error_false_two", "错误答案2长度：1-250个字符");
			flag = false;
		}
		
		if(false_three == null || "".equals(false_three)) {
			model.put("error_false_three", "请输入错误答案3");
			flag = false;
		}
		
		if(false_three.length() > 250) {
			model.put("error_false_three", "错误答案3长度：1-250个字符");
			flag = false;
		}
		
		if( difficulty == 0){
			model.put("error_difficulty", "请选择难度");
			flag = false;
		}
		
		if(score == null){
			model.put("error_score", "请输入分值");
			flag = false;
		}else{
			if(score<1 || score>99){
				model.put("error_score", "分值须为1-99的整数");
				flag = false;
			}
		}
		
		if( chapterID == 0){
			model.put("error_chapterID", "请选择章节");
			flag = false;
		}
		
		if( remark == null || "".equals(remark) ){
			xuanze.setRemark(null);
        }else{
        	if(remark.length() > 100) {
    			model.put("error_remark", "备注长度：0-100个字符");
    			flag = false;
    		}
        }

		//校验不通过
        if(!flag){
        	model.put("chapterList", chapterList);
			model.put("pic_url", pic_url);
			return new ModelAndView(getFormView(), model);
		}
		
		boolean isEditSuccess = questionService.editXuanzeInfo(multipartRequest, xuanze, model);
		
		if (isEditSuccess) {
			return new ModelAndView(getSuccessView());
		} else {
			model.put("chapterList", chapterList);
			model.put("pic_url", pic_url);
			return new ModelAndView(getFormView(), model);
		}
	}


}
